﻿Ext.gesture.Touch = Ext.extend(Ext.gesture.Gesture, {
    handles: ['touchstart', 'touchmove', 'touchend', 'touchdown'],

    touchDownInterval: 500,

	//触摸屏幕开始时刻触发
    onTouchStart: function (e, touch) {
        this.startX = this.previousX = touch.pageX;//记录开始点横坐标
        this.startY = this.previousY = touch.pageY;//记录开始点纵坐标
        this.startTime = this.previousTime = e.timeStamp;//开始时间戳

        this.fire('touchstart', e);//触发touchstart事件（gesture级别上的事件）
        this.lastEvent = e;

        if (this.listeners && this.listeners.touchdown) {
			//格500毫秒后触发this.touchDownHandler方法
            this.touchDownIntervalId = setInterval(Ext.createDelegate(this.touchDownHandler, this), this.touchDownInterval);
        }
    },

    onTouchMove: function (e, touch) {
        this.fire('touchmove', e, this.getInfo(touch));
        this.lastEvent = e;
    },

    onTouchEnd: function (e) {
        this.fire('touchend', e, this.lastInfo);
        clearInterval(this.touchDownIntervalId);
    },

    touchDownHandler: function () {
        this.fire('touchdown', this.lastEvent, this.lastInfo);
    },

	//获取事件详细信息（touch为手机事件中的e.changedTouches中的事件）
    getInfo: function (touch) {
        var time = Date.now(),
            deltaX = touch.pageX - this.startX,
            deltaY = touch.pageY - this.startY,
            info = {
                startX: this.startX,
                startY: this.startY,
                previousX: this.previousX,
                previousY: this.previousY,
                pageX: touch.pageX,
                pageY: touch.pageY,
                deltaX: deltaX,
                deltaY: deltaY,
                absDeltaX: Math.abs(deltaX),
                absDeltaY: Math.abs(deltaY),
                previousDeltaX: touch.pageX - this.previousX,
                previousDeltaY: touch.pageY - this.previousY,
                time: time,
                startTime: this.startTime,
                previousTime: this.previousTime,
                deltaTime: time - this.startTime,
                previousDeltaTime: time - this.previousTime
            };

        this.previousTime = info.time;
        this.previousX = info.pageX;
        this.previousY = info.pageY;
        this.lastInfo = info;

        return info;
    }
});

Ext.regGesture('touch', Ext.gesture.Touch);